package com.wjy.common.config.mybatisplus;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ybxxszl
 * @desc MyBatisPlus插件配置
 * @date 2019/4/14
 */
@Configuration
public class MyBatisPlusConfig {

    /*
     * 逻辑删除
     */
    @Bean
    public ISqlInjector sqlInjector() {
        LogicSqlInjector logicSqlInjector = new LogicSqlInjector();
        return logicSqlInjector;
    }

    /*
     * 乐观锁
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        OptimisticLockerInterceptor optimisticLockerInterceptor = new OptimisticLockerInterceptor();
        return optimisticLockerInterceptor;
    }

    /*
     * SQL执行效率，开发、测试环境开启，生产环境关闭
     */
    // @Bean
    // @Profile(value = "dev")
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();

        // 格式化SQL
        performanceInterceptor.setFormat(true);
        // 写入日志
        performanceInterceptor.setWriteInLog(false);
        // SQL最大执行时间，超过打印错误日志并停止运行
        performanceInterceptor.setMaxTime(2000);

        return performanceInterceptor;
    }

}
